<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>demo7 - 1</title>
    <!-- 引用在线的Vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
    <!-- 引用在线的Vuex.js -->
    <script src="https://cdn.jsdelivr.net/npm/vuex@3.6.2/dist/vuex.js"></script>
</head>

<body>
    <div id="app">
        <button @click="add">+</button>
        <span>{{this.$store.state.count}}</span>
        <button @click="sub">-</button>
    </div>
    <script>
        // 创建对象store
        const store = new Vuex.Store({
            // count初始值
            state: {
                count: 0
            },
            // 修改变量count的函数
            mutations: {
                add(state) {
                    state.count++
                },
                sub(state) {
                    state.count--
                }
            }
        })
        vm = new Vue({
            el: '#app',
            store,//挂载vm实例
            methods: {
                add() {
                    this.$store.commit('add')
                },
                sub() {
                    this.$store.commit('sub')
                }
            }
        })
    </script>
</body>

</html>